<?php

    class ControllerAjaxUploader extends Controller{
        
        private $name;
                
        public function upload(){
            
            $uploaddir = DIR_IMAGE. 'data/upload/'; 
            $json = array();
            
            if (isset($this->request->files['uploadfile']) && $this->request->files['uploadfile']['tmp_name']) {
                
                $file_size = $this->request->files['uploadfile']['size'];

                if($file_size > 500000){
                    $json['error'] = "File length is less than 5M"; 
                    $this->response->setOutput(json_encode($json));
                    return;
                }
                
                $allowed = array(
                                'image/jpeg',
                                'image/pjpeg',
                                'image/png',
                                'image/rar',
                				'image/tar',
                                'image/gif',                                
				);
						
                if (!in_array($this->request->files['uploadfile']['type'], $allowed)) {                        
                        $json['error'] = "File is not match!";
                        $this->response->setOutput(json_encode($json));
                        return;
                }      
                
                
                if(!file_exists($uploaddir)){
                    if(!mkdir($uploaddir)){
                        $json['error'] = "Error!";
                        $this->response->setOutput(json_encode($json));
                        return;                  
                    }
                }
                
                $temp =  $this->request->files['uploadfile']['tmp_name'];

                if (move_uploaded_file($temp, $uploaddir)) { 
                   
                    $json['status'] =  'OK';
                    
                } else {
                    
                   $json['error'] = "Upload file not success !";                    
                }
                
                $this->response->setOutput(json_encode($json));
            }            
        }
    }
?>